package com.it.gw.modules.map.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Toast
import com.amap.api.maps.AMap
import com.amap.api.maps.AMap.OnMarkerClickListener
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.MyLocationStyle
import com.it.common.bean.MapDataBean
import com.it.common.utils.GsonUtils
import com.it.common.utils.MapUtils
import com.it.common.utils.isNotEmpty
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityMapBinding
import java.io.Serializable


class MapActivity : BaseActivity<ActivityMapBinding>() {
    companion object {
        const val TAG = "MapActivity"
        const val MAP_DATA = "MAP_DATA"
    }

    override fun initObserver() {

    }

    var lttd = 0.0
    var lgtd = 0.0
    override fun initListener() {
        binding.ivNav.setOnClickListener {
            if (binding.name.text.toString().isNullOrEmpty() || lgtd == 0.0 || lttd == 0.00) {
                Toast.makeText(this, "请在地图上选择导航的地点", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            navGaoDeToLocation(
                latitude = lttd,
                longitude = lgtd,
                address = binding.name.text.toString()
            )

        }
        binding.rlOk.setOnClickListener {
            val intent = Intent()
            intent.putExtra("lgtd",lgtd.toString())
            intent.putExtra("lttd",lttd.toString())
            setResult(RESULT_OK, intent)
            finish()

        }

    }

    /**
     * 高德地图导航 t 0 驾车    2 步行  3 骑车
     * @param latitude Double
     * @param longitude Double
     * @param address String
     */
    private fun navGaoDeToLocation(latitude: Double, longitude: Double, address: String) {
        if (!MapUtils.isAvilible(this, "com.autonavi.minimap")) {
            Toast.makeText(this, "请安装高德地图", Toast.LENGTH_LONG).show()
            return
        }
        val uri =
            Uri.parse("amapuri://route/plan/?dlat=$latitude&dlon=$longitude&dname=$address&dev=0&t=0")
        startActivity(Intent(Intent.ACTION_VIEW, uri))
    }

    override fun initData() {

    }

    private var mapList = mutableListOf<MapDataBean>()
    override fun initView() {
        setTitle(
            title = "${intent.getStringExtra("title")}",
            isBack = true
        )
       val dataType =  intent.getStringExtra("dataType")
        if(TextUtils.isEmpty(dataType)){
            binding.rlOk.visibility = View.GONE
        }
        else{
            binding.rlOk.visibility = View.VISIBLE
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.map.onCreate(savedInstanceState) // 此方法必须重写
        val data = intent.getStringExtra(MAP_DATA)
        if (data.isNotEmpty()) {
            mapList = GsonUtils.jsonStrToObj<List<MapDataBean>>(data!!).toMutableList()
            val aMap: AMap = binding.map.map
            val myLocationStyle = MyLocationStyle()
            myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//指定位一次
            aMap.myLocationStyle = myLocationStyle //设置定位蓝点的Style


            mapList.forEach { data ->
                val latLng = LatLng(data.lttd!!.toDouble(), data.lgtd!!.toDouble())
                aMap.addMarker(MarkerOptions().position(latLng).title(data.name))

            }

            //移动到第一个点
            aMap.moveCamera(CameraUpdateFactory.newLatLng(LatLng(mapList[0].lttd!!.toDouble(), mapList[0].lgtd!!.toDouble())));


            // 定义 Marker 点击事件监听
            val markerClickListener = OnMarkerClickListener {
                Log.d(
                    TAG,
                    "OnMarkerClickListener: ${it.title} ${it.position.latitude} ${it.position.longitude}"
                )
                binding.name.text = it.title
                lttd = it.position.latitude
                lgtd = it.position.longitude
                binding.longitude.text ="当前经度：$lgtd"
                binding.latitude.text ="当前纬度：$lttd"
                false
            }

            aMap.setOnMarkerClickListener(markerClickListener)
            aMap.setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
                override fun onCameraChange(p0: CameraPosition?) {

                }

                override fun onCameraChangeFinish(cameraPosition: CameraPosition?) {
                    // 地图移动结束后的回调，此时可以获取到最终的地图中心点经纬度
                    // 地图移动结束后的回调，此时可以获取到最终的地图中心点经纬度
                    val latitude = cameraPosition?.target?.latitude
                    val longitude = cameraPosition?.target?.longitude
                    if(latitude!=null){
                        lttd = latitude
                    }
                    if(longitude!=null){
                        lgtd = longitude
                    }


                    binding.longitude.text ="当前经度：$longitude"
                    binding.latitude.text ="当前纬度：$latitude"


                }

            })
            if (mapList.size == 1) {
                binding.name.text = mapList.get(0).name
                lttd = mapList[0].lttd!!.toDouble()
                lgtd = mapList[0].lgtd!!.toDouble()
                binding.longitude.text ="当前经度：$lgtd"
                binding.latitude.text ="当前纬度：$lttd"


            }

        }




    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        binding.map.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        binding.map.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.map.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.map.onSaveInstanceState(outState)
    }

    override fun setContentView() {
        binding = ActivityMapBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}